
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


/*
    SHA1 
    Change Namespace to miniutil::util
    Wang Bo
*/

#ifndef ___MINIUTIL_UTIL_SHA1_H___
#define ___MINIUTIL_UTIL_SHA1_H___


#include <string>
#include "miniutil/buffer.h"

namespace miniutil
{
    namespace util
    {

        typedef unsigned int t_uint32;

        class CSHA1
        {
        public:

	        // Two different formats for ReportHash(...)
	        enum { REPORT_HEX = 0, REPORT_DIGIT = 1 };

	        // Constructor and Destructor
	        CSHA1();
	        virtual ~CSHA1();

	        t_uint32 m_state[5];
	        t_uint32 m_count[2];
	        unsigned char m_buffer[64];
	        unsigned char m_digest[20];

	        void Reset();

	        // Update the hash value
	        void Update(unsigned char* data, unsigned int len);

	        // Finalize hash and report
	        void Final();
	        void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
	        void GetHash(unsigned char *uDest);

        private:
	        // Private SHA-1 transformation
	        void Transform(t_uint32 state[5], unsigned char buffer[64]);

        public:
            static inline std::string raw_digest( const char * data, int len = -1 )
            {
                CSHA1 sha1_hash;
                if ( len <= 0 ) len = (int) strlen( data );
                sha1_hash.Update( (unsigned char *)data, (unsigned int)len );
                sha1_hash.Final();

                unsigned char buf[20];
                sha1_hash.GetHash( buf );

                return std::string( (char *)buf, 20 );
            }
            static inline std::string raw_digest( const std::string & data )
            {
                return raw_digest( data.data(), (int) data.length() );
            }

            static inline std::string hex_digest( const char * data, int len = -1 )
            {
                CSHA1 sha1_hash;
                if ( len <= 0 ) len = (int) strlen( data );
                sha1_hash.Update( (unsigned char *)data, (unsigned int)len );
                sha1_hash.Final();

                char buf[50] = {0};
                sha1_hash.ReportHash( buf );

                return std::string( ( const char *) buf, 40 );
            }
            static inline std::string hex_digest( const std::string & data )
            {
                return hex_digest( data.data(), (int)data.length() );
            }
        };


    }
}

#endif // ___SHA1_H___
